package com.cskaoyan.service;

import com.cskaoyan.bean.Storage;
import com.cskaoyan.bean.StorageListBean;
import com.cskaoyan.bean.StorageVO;
import com.cskaoyan.bean.params.BaseParam;
import com.cskaoyan.config.AliyunComponent;
import com.cskaoyan.mapper.StorageMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @description：
 * @author: 曹旭
 * @date: 2022/5/11 07:08
 */
@Service
public class StorageServiceBAKImpl implements StorageServiceBAK {
    @Autowired
    AliyunComponent aliyunComponent;

    @Autowired
    StorageMapper storageMapper;

    @SneakyThrows
    @Override
    public StorageVO storage(MultipartFile file) {
        String key = UUID.randomUUID().toString().replaceAll("-","") + ".png";
        String name = file.getOriginalFilename();
        String type = file.getContentType();
        int size = (int)file.getSize();
        String url = aliyunComponent.fileUpload(file);
        Date date = new Date();

        Storage storage = new Storage(null, key, name, type, size, url, date, date, false);
        int affectedRows = storageMapper.insertSelective2(storage);
        if (affectedRows==1){
            StorageVO storageVO = new StorageVO(storage.getId(), storage.getKey(),storage.getName(),storage.getType(),storage.getSize(),storage.getUrl(),storage.getAddTime(),storage.getUpdateTime());
            return storageVO;
        }else{
            return null;
        }
    }

    @Override
    public StorageListBean list(String name, String key, BaseParam baseParam) {
        //查询总共所有的数量
        Integer page = baseParam.getPage();//页码
        Integer limit = baseParam.getLimit();//数据量
        String sort = baseParam.getSort();//排序列
        String order = baseParam.getOrder();//desc\asc
        //分页插件 PageHelper，辅助我们做分页以及分页信息的获得
        PageHelper.startPage(page, limit);
        //执行查询过程中拼接分页信息
        //  username="%"+username+"%";
        List<Storage> list = storageMapper.select(sort,order,name,key);
        //total、pages、list
        //通过PageHelper帮我们来获得一些和分页相关的信息PageInfo
        //使用其有参构造方法 → 形参写来源于数据库的list
        PageInfo<Storage> pageInfo = new PageInfo<>(list);
        long total = pageInfo.getTotal();
        int pages = pageInfo.getPages();
        return StorageListBean.data(total, pages, limit, page, list);
    }

    @Override
    public int delete(Storage storage) {
        //虚假删除
        Storage newStorage = new Storage(storage.getId(), null, null, null, null, null, null, new Date(System.currentTimeMillis()), true);
        int affectedRows = storageMapper.updateByPrimaryKeySelective(newStorage);
        if (affectedRows==1){
            return 200;
        }
        return 0;
    }

    @Override
    public Storage update(Storage storage) {
        Storage newStorage = new Storage(storage.getId(), null, storage.getName(), null, null, null, null,
                new Date(System.currentTimeMillis()), null);
        int affectedRows = storageMapper.updateByPrimaryKeySelective(newStorage);
        if (affectedRows==1){
            return newStorage;
        }
        return null;
    }
}
